﻿namespace EasyCMS.WebUtils.UI
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using Spring.Aop.Framework;
    using Spring.Aop;
    using System.Reflection;

    public class Page : Pismire.Web.UI.Page
    {
        protected EasyCMS.Common.ICategorys Categorys
        {
            get
            {
                if (m_Categorys == null)
                {
                    m_Categorys = EasyCMS.Common.Runtime.ObjectContainer.Resolve<EasyCMS.Common.ICategorys>();
                }
                return m_Categorys;
            }
        }
        private EasyCMS.Common.ICategorys m_Categorys;

        protected EasyCMS.Common.ISubjects Subjects
        {
            get
            {
                if (m_Subjects == null)
                {
                    m_Subjects = EasyCMS.Common.Runtime.ObjectContainer.Resolve<EasyCMS.Common.ISubjects>();
                }
                return m_Subjects;
            }
        }
        private EasyCMS.Common.ISubjects m_Subjects;

        protected EasyCMS.Common.IContents Contents
        {
            get
            {
                if (m_Contents == null)
                {
                    m_Contents = EasyCMS.Common.Runtime.ObjectContainer.Resolve<EasyCMS.Common.IContents>();
                }
                return m_Contents;
            }
        }
        private EasyCMS.Common.IContents m_Contents;

        protected EasyCMS.Common.IComments Comments
        {
            get
            {
                if (m_Comments == null)
                {
                    m_Comments = EasyCMS.Common.Runtime.ObjectContainer.Resolve<EasyCMS.Common.IComments>();
                }
                return m_Comments;
            }
        }
        private EasyCMS.Common.IComments m_Comments;

        protected EasyCMS.Common.IMembers Members
        {
            get
            {
                if (m_Members == null)
                {
                    m_Members = EasyCMS.Common.Runtime.ObjectContainer.Resolve<EasyCMS.Common.IMembers>();
                }
                return m_Members;
            }
        }
        private EasyCMS.Common.IMembers m_Members;

        protected EasyCMS.Common.ITests Tests
        {
            get
            {
                if (m_Tests == null)
                {
                    m_Tests = EasyCMS.Common.Runtime.ObjectContainer.Resolve<EasyCMS.Common.ITests>();

                    ProxyFactory factory = new ProxyFactory(m_Tests);
                    factory.AddAdvice(new AspectTestByBefore());
                    factory.AddAdvice(new AspectTestByReturning());
                    m_Tests = (EasyCMS.Common.ITests)factory.GetProxy();
                    
                }
                return m_Tests;
            }
        }
        private EasyCMS.Common.ITests m_Tests;
    }

    public class AspectTestByBefore : IMethodBeforeAdvice
    {
        public void Before(MethodInfo method, object[] args, object target)
        {
            System.Web.HttpContext.Current.Response.Write("<br />");
            System.Web.HttpContext.Current.Response.Write("Intercepted call to this method : " + method.Name);
            System.Web.HttpContext.Current.Response.Write("    The target is       : " + target);
            System.Web.HttpContext.Current.Response.Write("    The arguments are   : ");
            if (args != null)
            {
                foreach (object arg in args)
                {
                    System.Web.HttpContext.Current.Response.Write("\t: " + arg);
                }
            }
            System.Web.HttpContext.Current.Response.Write("<br />");
        }
    }

    public class AspectTestByReturning : IAfterReturningAdvice
    {
        public void AfterReturning(
            object returnValue, MethodInfo method, object[] args, object target)
        {
            System.Web.HttpContext.Current.Response.Write("<br />");
            System.Web.HttpContext.Current.Response.Write("This method call returned successfully : " + method.Name);
            System.Web.HttpContext.Current.Response.Write("    The target was      : " + target);
            System.Web.HttpContext.Current.Response.Write("    The arguments were  : ");
            if (args != null)
            {
                foreach (object arg in args)
                {
                    System.Web.HttpContext.Current.Response.Write("\t: " + arg);
                }
            }
            System.Web.HttpContext.Current.Response.Write("    The return value is : " + returnValue);

            System.Web.HttpContext.Current.Response.Write("<br />");
        }
    }
}
